package com.arcvideo.acsplayer.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import android.widget.Toast;

import com.arcvideo.acsplayer.R;
import com.arcvideo.acsplayer.config.ApplicationConfig;
import com.arcvideo.acsplayer.dialog.NormalDialog;
import com.arcvideo.acsplayer.dialog.NormalDialog.ClickListener;
import com.arcvideo.acsplayer.utils.NetUtil;

public class BaseActivity extends Activity {
	private static final String LOG_TAG = "BaseActivity";
	private Dialog mRemandDialog = null;
	private boolean mIsClosing = false;
	private boolean mIsPause = false;
	private boolean mIsShowing = false;
	private int mAirState = 0;

	private BroadcastReceiver m_wifiReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (mIsClosing)
				return;
			if (mIsShowing)
				return;

			String action = intent.getAction();
			if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
				ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
				NetworkInfo wifiInfo = (NetworkInfo) connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
				NetworkInfo mobileInfo = (NetworkInfo) connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

				if (mobileInfo == null) {
					if (wifiInfo != null && State.DISCONNECTED == wifiInfo.getState()) {
						// WIFI disconnected
						if (!mIsPause) {
							mIsPause = false;
							mIsShowing = true;
							ShowRemandDialog(R.string.net_error, R.string.net_error_info);
						}
					}
				} else {
					if (wifiInfo != null) {
						if (State.CONNECTED == wifiInfo.getState() && State.CONNECTED != mobileInfo.getState()) {
							// WIFI connected
						} else if (State.CONNECTED != wifiInfo.getState() && State.CONNECTED == mobileInfo.getState()) {
							// 2G/3G/4G connected
							if (!mIsPause) {
								mIsPause = false;
								Toast.makeText(context, R.string.net_error_info2, Toast.LENGTH_SHORT);
							}
						} else if (State.CONNECTED != wifiInfo.getState() && State.CONNECTED != mobileInfo.getState()) {
							// no any network
							if (!mIsPause) {
								mIsPause = false;
								mIsShowing = true;
								ShowRemandDialog(R.string.net_error, R.string.net_error_info);
							}
						}
					}
				}
			} else if (action.equals(ApplicationConfig.NET_ERROR)) {
				if (!NetUtil.isWiFiActive(BaseActivity.this)) {
					Toast.makeText(BaseActivity.this, getResources().getText(R.string.net_error_info3), Toast.LENGTH_LONG).show();
				}
			} else if (action.equals("android.intent.action.SERVICE_STATE")) {
				Bundle bundle = intent.getExtras();
				if (bundle != null) {
					mAirState = bundle.getInt("state");

					Log.e(LOG_TAG, "·ÉÐÐÄ£Ê½×´Ì¬ 1Îª¿ªÆô×´Ì¬£¬0Îª¹Ø±Õ×´Ì¬ airState==" + mAirState);
					switch (mAirState) {
						case 0 : // air mode close success
							Log.i(LOG_TAG, "close state  airState=" + mAirState);
							break;
						case 1 : // air mode opening
							Log.i(LOG_TAG, "opening airState=" + mAirState);
							break;
						case 3 : // air mode open success
							Log.i(LOG_TAG, "open state  airState=" + mAirState);
							if (!mIsPause) {
								mIsPause = false;
								mIsShowing = true;
								ShowRemandDialog(R.string.net_error, R.string.net_error_info);
							}
							break;
					}
				}
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		registerNetReceiver();

		if (!NetUtil.isNetworkAvailable(this)) {
			mIsShowing = true;
			ShowRemandDialog(R.string.net_error, R.string.net_error_info);
		}
	}

	@Override
	protected void onResume() {
		mIsPause = false;
		super.onResume();
	}

	@Override
	protected void onPause() {
		mIsPause = true;
		if (null != mRemandDialog) {
			mRemandDialog.dismiss();
		}
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		unRegisterNetReceiver();

		mIsClosing = true;

		super.onDestroy();
	}

	private void registerNetReceiver() {
		IntentFilter wifiFilter = new IntentFilter();
		wifiFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		wifiFilter.addAction(ApplicationConfig.NET_ERROR);
		wifiFilter.addAction("android.intent.action.SERVICE_STATE");
		registerReceiver(m_wifiReceiver, wifiFilter);
	}

	private void unRegisterNetReceiver() {
		unregisterReceiver(m_wifiReceiver);
	}

	public void ShowRemandDialog(int titleId, int messageId) {
		if (null != mRemandDialog && mRemandDialog.isShowing())
			mRemandDialog.hide();
		try {
			mRemandDialog = new NormalDialog(BaseActivity.this, R.style.no_title_dilog, titleId, messageId, true, mRemandDialogInterface);
			mRemandDialog.show();
			WindowManager windowManager = getWindowManager();
			Display display = windowManager.getDefaultDisplay();
			WindowManager.LayoutParams lp = mRemandDialog.getWindow().getAttributes();

			if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
				lp.height = (int) (display.getHeight() * 0.55); // ¸ß¶ÈÉèÖÃÎªÆÁÄ»µÄ0.3
				lp.width = (int) (display.getWidth() * 0.6); // ¿í¶ÈÉèÖÃÎªÆÁÄ»µÄ0.5
			} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
				lp.height = (int) (display.getHeight() * 0.35); // ¸ß¶ÈÉèÖÃÎªÆÁÄ»µÄ0.3
				lp.width = (int) (display.getWidth() * 0.8); // ¿í¶ÈÉèÖÃÎªÆÁÄ»µÄ0.5
			}
			mRemandDialog.getWindow().setAttributes(lp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private ClickListener mRemandDialogInterface = new ClickListener() {

		@Override
		public void confirm() {
			mRemandDialog.dismiss();
			mIsShowing = false;
		}

		@Override
		public void cancel() {
			mRemandDialog.dismiss();
			mIsShowing = false;
		}

		@Override
		public void remand() {
			mRemandDialog.dismiss();
			mIsShowing = false;
			BaseActivity.this.sendBroadcast(new Intent(ApplicationConfig.DIALOG_DISMISS));
		}
	};
}